﻿namespace WebHoiChoMuaBan.Controllers.Helpers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using WebHoiChoMuaBan.Models;

    public class AdminHelper : HelperBase
    {
        public void addGroup(Group grp)
        {
            base.dataContext.Groups.InsertOnSubmit(grp);
            this.Save();
        }

        public void addGroupRight(int grpId, List<int> rightId, List<int> lstTemplate)
        {
            this.deleteGroupRightByGroupId(grpId);
            for (int i = 0; i < rightId.Count; i++)
            {
                Group_Right gr = new Group_Right
                {
                    GroupID = grpId
                };
                if (rightId[i] == -1)
                {
                    Right r = this.getRightByRightName("1");
                    gr.RightID = r.RightID;
                }
                else
                {
                    gr.RightID = rightId[i];
                }
                if (lstTemplate[i] == -1)
                {
                    gr.TemplateId = null;
                }
                else
                {
                    gr.TemplateId = new int?(lstTemplate[i]);
                }
                base.dataContext.Group_Rights.InsertOnSubmit(gr);
                this.Save();
            }
        }

        public void addMember(int memId, int grpId)
        {
            Member_Group mem = new Member_Group
            {
                GroupID = grpId,
                MemID = memId
            };
            base.dataContext.Member_Groups.InsertOnSubmit(mem);
            base.dataContext.SubmitChanges();
        }

        public bool deleteGroup(int grpId)
        {
            if ((from e in base.dataContext.Member_Groups
                 where e.GroupID == grpId
                 select e).ToList<Member_Group>().Count > 0)
            {
                return false;
            }
            List<Group_Right> gr = (from e in base.dataContext.Group_Rights
                                    where e.GroupID == grpId
                                    select e).ToList<Group_Right>();
            base.dataContext.Group_Rights.DeleteAllOnSubmit<Group_Right>(gr);
            base.dataContext.Groups.DeleteOnSubmit((from e in base.dataContext.Groups
                                                    where e.GroupID == grpId
                                                    select e).SingleOrDefault<Group>());
            base.dataContext.SubmitChanges();
            return true;
        }

        public void deleteGroupRightByGroupId(int grp)
        {
            List<Group_Right> gr = (from e in base.dataContext.Group_Rights
                                    where e.GroupID == grp
                                    select e).ToList<Group_Right>();
            base.dataContext.Group_Rights.DeleteAllOnSubmit<Group_Right>(gr);
            this.Save();
        }

        public List<Member> getAllAdminMember()
        {
            return (from e in base.dataContext.Members
                    where e.MemTypeId == 6
                    select e).ToList<Member>();
        }

        public List<Group> getAllGroup()
        {
            return (from e in base.dataContext.Groups
                    orderby e.Scope
                    select e).ToList<Group>();
        }

        public List<Group_Right> getAllGroupRightByGroupId(int grpID)
        {
            return (from e in base.dataContext.Group_Rights
                    where e.GroupID == grpID
                    select e).ToList<Group_Right>();
        }

        public List<Member> getAllMemberByGroupId(int grpId)
        {
            List<Member_Group> mg = (from e in base.dataContext.Member_Groups
                                     where e.GroupID == grpId
                                     select e).ToList<Member_Group>();
            List<Member> mem = new List<Member>();
            foreach (Member_Group m in mg)
            {
                mem.Add(new MemberHelper().getMember(m.MemID));
            }
            return mem;
        }

        public List<Right> getAllRight()
        {
            return (from e in base.dataContext.Rights
                    orderby e.RightName
                    select e).ToList<Right>();
        }

        public List<Right> getAllRightByGroupId(int groupId)
        {
            return (from e in base.dataContext.Group_Rights
                    where e.GroupID == groupId
                    select e.Right).ToList<Right>();
        }

        public List<Template> getAllTemplate()
        {
            return (from e in base.dataContext.Templates
                    orderby e.TemId
                    select e).ToList<Template>();
        }

        public List<Template> getAllTemplatebyGroupAndRight(int groupId, List<Right> right)
        {
            List<Template> result = new List<Template>();
            foreach (Right r in right)
            {
                List<Template> tmp = (from e in this.dataContext.Group_Rights where e.GroupID == groupId && e.RightID == r.RightID select e.Template).ToList<Template>();
                if (tmp.Count != 0)
                {
                    foreach (Template t in tmp)
                    {
                        result.Add(t);
                    }
                }
            }
            return result;

        }


        public Group getGroupByMemberId(int memId)
        {
            return (from e in base.dataContext.Member_Groups
                    where e.MemID == memId
                    select e.Group).SingleOrDefault<Group>();
        }

        public Right getRightByRightName(string rightName)
        {
            return (from e in base.dataContext.Rights
                    where e.RightName.Equals(rightName)
                    select e).SingleOrDefault<Right>();
        }

        public int? getTemplateID(int grp, int rht)
        {
            Group_Right gr = (from e in base.dataContext.Group_Rights
                              where (e.RightID == rht) && (e.GroupID == grp)
                              select e).SingleOrDefault<Group_Right>();
            int? rid = gr.TemplateId;
            if (rid == null)
            {
                return null;
            }
            return gr.TemplateId;
        }

        public void removeMember(int memId, int grpId)
        {
            Member_Group mem = new Member_Group
            {
                MemID = memId,
                GroupID = grpId
            };
            base.dataContext.Member_Groups.Attach(mem);
            base.dataContext.Member_Groups.DeleteOnSubmit(mem);
            base.dataContext.SubmitChanges();
        }

        public void Save()
        {
            base.dataContext.SubmitChanges();
        }

        public void updateGroup(int grpId, string desc, string name)
        {
            Group gr = (from e in base.dataContext.Groups
                        where e.GroupID == grpId
                        select e).SingleOrDefault<Group>();
            gr.GroupName = name;
            gr.Description = desc;
            base.dataContext.SubmitChanges();
        }
    }
}

